﻿using System;
using System.Net;
using System.Web.Mvc;

namespace MvcFlow.Mvc.ActionResults
{
    [Serializable]
    public class HttpStatusActionResult : IActionResult
    {
        private readonly HttpStatusCode statusCode;
        private readonly string         statusDescription;

        public ActionResult Bind(ControllerBase controllerBase)
        {
            return string.IsNullOrWhiteSpace(statusDescription)
                       ? new HttpStatusCodeResult(statusCode)
                       : new HttpStatusCodeResult(statusCode, statusDescription);
        }

        public static HttpStatusActionResult FromError( string statusDescription,bool isDebugging )
        {
            return FromError(HttpStatusCode.InternalServerError, statusDescription, isDebugging);
        }

        public static HttpStatusActionResult FromError( HttpStatusCode statusCode,string statusDescription,bool isDebugging )
        {
            return new HttpStatusActionResult(statusCode,isDebugging ? statusDescription : null);
        }

        public static HttpStatusActionResult FromException(Exception ex,bool isDebugging)
        {
            return FromException(HttpStatusCode.InternalServerError, ex, isDebugging);
        }

        public static HttpStatusActionResult FromException( HttpStatusCode statusCode,Exception ex,bool isDebugging )
        {
            return new HttpStatusActionResult(statusCode,isDebugging ? ex.ToString() : null);
        }

        public HttpStatusActionResult(HttpStatusCode statusCode,string statusDescription = null)
        {
            this.statusCode         = statusCode;
            this.statusDescription  = statusDescription;
        }
    }
}